package com.ptma.controllers;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.StringReader;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.ptma.model.ApplicationVersion;
import com.ptma.model.Device;
import com.ptma.model.VersionComment;
import com.ptma.repository.GenericRepo;

/**
 * 
 * @author yahya
 * 
 */
@Controller
@RequestMapping(value = "/app/mobile")
public class MobileRequestsHandler extends AbstractController {

	@Autowired
	private GenericRepo<Device, Long> deviceService;
	@Autowired
	private GenericRepo<VersionComment, Long> defectService;
	@Autowired
	private GenericRepo<ApplicationVersion, Long> appService;

	@RequestMapping(value = "/authenticate", method = RequestMethod.POST)
	public @ResponseBody
	Response autheticate(HttpServletRequest request,
			HttpServletResponse response) {
		return null;
	}

	@RequestMapping(value = "/applications", method = RequestMethod.POST)
	public @ResponseBody
	Response androidApks(HttpServletRequest request,
			HttpServletResponse response) {

		Response responseXml = new Response();
		responseXml.setStatusCode(Response.OK);

		Device device = (Device) request.getSession()
				.getAttribute("MobileUser");

		// List<Application> apps = deviceService.getAllApplications(device);
		// device.setApplications(apps);
		responseXml.setResponseBody(device);

		return responseXml;
	}

	@RequestMapping(value = "/getApk", method = RequestMethod.POST)
	public @ResponseBody
	byte[] getApplication(HttpServletRequest request,
			HttpServletResponse response) {

		try {
			Device device = (Device) request.getSession().getAttribute(
					"MobileUser");
			if (device != null) {
				ApplicationContext applicationContext = WebApplicationContextUtils
						.getWebApplicationContext(request.getSession()
								.getServletContext());
				InputStream in = applicationContext.getResource(
						"/resources/images/2.apk").getInputStream();

				return IOUtils.toByteArray(in);
			}
		} catch (Exception e) {
			e.printStackTrace();

		}

		return null;
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public @ResponseBody
	Response login(HttpServletRequest request, HttpServletResponse response) {

		return null;
	}

	@RequestMapping(value = "/submitDefect", method = RequestMethod.POST)
	public @ResponseBody
	Response submitDefect(HttpServletRequest request,
			HttpServletResponse response) {
		Response res = new Response();
		try {

			BufferedReader reader = request.getReader();
			StringBuilder xml = new StringBuilder(100);
			String line = null;
			while ((line = reader.readLine()) != null) {
				xml.append(line);
			}
			JAXBContext context = JAXBContext.newInstance(VersionComment.class);

			Unmarshaller um = context.createUnmarshaller();
			VersionComment defect = (VersionComment) um
					.unmarshal(new StringReader(xml.toString()));
			Device device = (Device) request.getSession().getAttribute(
					"MobileUser");

			// defect.setApplicationVersion(defect.getApplicationVersion());
			// defectService.save(defect);
			res.setStatusCode(Response.OK);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	@RequestMapping(value = "/updateAppInstallStatus", method = RequestMethod.POST)
	public @ResponseBody
	Response updateInstallStatus(HttpServletRequest request,
			HttpServletResponse response) {
		return null;
	}
}
